package com.oceansoft.mobile.econsole.vehicle.service;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.json.JSONArray;
import org.json.JSONObject;
import com.oceansoft.mobile.econsole.common.util.MD5Util;

/**
 * 违法查询接口调用
 * @author wangjj
 *
 */
public class TrafficVehicleService {
	private static final String wsdlUrl ="http://221.224.161.122:8095/Traffic/Traffic.Interface.Out/TrafficService.asmx";
    private static final String nameSpaceUri = "http://tempuri.org/";
    private long time = 0L;
    private String expires_in;//有效时间（s），默认7200
    private String token;//接入身份凭证
    private static TrafficVehicleService instance;
    
    public static synchronized TrafficVehicleService getInstance(){
    	if(instance==null){
    		instance = new TrafficVehicleService();
    	}
		return instance;
    }
    
    /**
     * 初始化获取token，当时间超过7200s时，重新获取token   
     */
    public void init(){
    	if(time == 0L){
    		getToken();
    	}else{
    		long cTime = System.currentTimeMillis();
            long sjc = (cTime-time);
            long stime = (Long.parseLong(expires_in))*1000;
            if(sjc>stime){
            	getToken();
            }
    	}
    }
   
    /**
     * 获取身份验证token
     */
    public void getToken(){  	
    	try {
    	Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(wsdlUrl);
		// 设置operation 名称，
		call.setOperationName(new QName(nameSpaceUri, "GetToken"));
		// 设置参数
		call.addParameter(new QName(nameSpaceUri, "App_ID"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "App_Secret"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "App_IP"), XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "App_Mac"), XMLType.XSD_STRING, ParameterMode.IN);
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);  
		call.setSOAPActionURI(nameSpaceUri+"GetToken");
		String pwd = MD5Util.MD5("OS-SZGA");
		String result = (String) call.invoke(new Object[] {"OS-SZGA",
					pwd,"221.224.13.5","B8-2A-72-CE-E3-FB"});
		
		JSONObject jobj=new JSONObject(result);
		token = jobj.getString("ACCESS_TOKEN");
		System.out.println(token);
		expires_in = jobj.getString("EXPIRES_IN");
        time = System.currentTimeMillis();
    	} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 获取车辆基本信息
     * @return
     * @throws Exception
     */
    public String getVehicleiInfo() throws Exception{    	
        init();
    	Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(wsdlUrl);
    	call.setOperationName(new QName(nameSpaceUri, "GetVehicleInfo"));
		// 设置参数
		call.addParameter(new QName(nameSpaceUri, "Token"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "HPZL"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "HPHM"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "CLSBDH"),XMLType.XSD_STRING, ParameterMode.IN);
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);  
		call.setSOAPActionURI(nameSpaceUri+"GetVehicleInfo");
		String result1 = (String) call.invoke(new Object[] {token,
				"07","ES6003","9112131"});
    	return result1;
    }
    
    /**
     * 获取驾驶证基本信息
     * @return
     * @throws Exception
     */
    public String getLicenseInfo(String jszh,String dabh) throws Exception{
    	init();
    	Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(wsdlUrl);
    	call.setOperationName(new QName(nameSpaceUri, "GetLicenseInfo"));
		// 设置参数
		call.addParameter(new QName(nameSpaceUri, "Token"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "SFZMHM"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "DABH"),XMLType.XSD_STRING, ParameterMode.IN);
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);  
		call.setSOAPActionURI(nameSpaceUri+"GetLicenseInfo");
		String result1 = (String) call.invoke(new Object[] {token,jszh,dabh});
				//"320520197903100416","320500417242";
    	return result1;
    }
    
    /**
     * 获取未处理的机动车交通违法信息
     * @return
     * @throws Exception
     */
    public String getVioSurveil(String hpzl,String hphm,String clsbdh) throws Exception{
    	init();
    	Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(wsdlUrl);
    	call.setOperationName(new QName(nameSpaceUri, "GetVioSurveil"));
		// 设置参数
		call.addParameter(new QName(nameSpaceUri, "Token"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "HPZL"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "HPHM"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "CLSBDH"),XMLType.XSD_STRING, ParameterMode.IN);
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);  
		call.setSOAPActionURI(nameSpaceUri+"GetVioSurveil");
		String result2 = (String) call.invoke(new Object[] {token,hpzl,hphm,clsbdh});
				//"02","EUM029","0072420";
    	return result2;
    }
    
    /**
     * 获取交通违法图片信息
     * @return
     * @throws Exception
     */
    public String getPhotoByXH(String xh) throws Exception{
    	init();
    	Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(wsdlUrl);
    	call.setOperationName(new QName(nameSpaceUri, "GetPhotoByXH"));
		// 设置参数
		call.addParameter(new QName(nameSpaceUri, "Token"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "XH"),XMLType.XSD_STRING, ParameterMode.IN);		
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);  
		call.setSOAPActionURI(nameSpaceUri+"GetPhotoByXH");
		String result3 = (String) call.invoke(new Object[] {token,
				xh});
    	return result3;
    }
    
    /**
     * 获取驾驶证交通违法信息
     * @return
     * @throws Exception
     */
    public String getViolation(String jszh,String dabh) throws Exception{
		init();
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(wsdlUrl);
		call.setOperationName(new QName(nameSpaceUri, "GetViolation"));
		// 设置参数
		call.addParameter(new QName(nameSpaceUri, "Token"), XMLType.XSD_STRING,ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "JSZH"),XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter(new QName(nameSpaceUri, "DABH"), XMLType.XSD_STRING,ParameterMode.IN);
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);
		call.setSOAPActionURI(nameSpaceUri + "GetViolation");
		String result4 = (String) call.invoke(new Object[] { token, "320520197903100416",
				"320500417242" });
		return result4;
    }
   
}
